<template>
	<view class="register">
		<text class="title">
			注册账号
		</text>
		<u-form :model="form" ref="uForm" class="form">
			<u-form-item label="账号">
				<u--input v-model="form.username" placeholder="请输入账号" />
			</u-form-item>
			<u-form-item label="密码">
				<u--input v-model="form.password" type="password" placeholder="请输入密码" />
			</u-form-item>
			<u-form-item label="确认">
				<u--input v-model="form.password2" type="password" placeholder="请再次输入密码" />
			</u-form-item>


		</u-form>
		<u-button class="button" @click="toNext">下一步</u-button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: '',
					password2: ''
				},
			};
		},
		methods: {
			toNext() {
				uni.request({
					header: {
						'content-type': 'application/json'
					},
					url: "http://127.0.0.1:8000/register/",
					method: "POST",
					data: {
						"username": this.form.username,
						"password": this.form.password,
						"password2": this.form.password2
					},
					success: (res) => {
						let data = res.data
						if (data.code === 1) {
							uni.setStorageSync("token", data.data.token)
							uni.setStorageSync("username", data.data.username)
							uni.navigateTo({
								url: '/pages/submit/submit',
							});
						} else {
							uni.showModal({
								content: data.errors,
								showCancel: false
							});

						}
					},
					fail: (err) => {
						uni.showModal({
							content: "服务器异常",
							showCancel: false
						});
					}
				})


			}
		}
	}
</script>

<style lang="scss">
	.register {


		.title {
			font-size: 50rpx;
			margin: 30px 50rpx;
		}

		.form {
			margin: 40px 30rpx;
		}

		.button {
			width: 600rpx;
			margin-top: 30px;
		}
	}
</style>
